/*
 * Copyright (C) 2010 TopCoder Inc., All Rights Reserved.
 */
package com.hercules.xre.protocol.testing.netty;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

import com.hercules.xre.protocol.testing.Helper;
import com.topcoder.util.log.Log;
import com.topcoder.util.log.LogManager;

/**
 * <p>
 * Channel handler that is responsible for logging channel exceptions.
 * </p>
 * <p>
 * <strong>Thread-safety:</strong> This class is immutable and thus thread safe.
 * </p>
 *
 * @author TCSDESIGNER, TCSDEVELOPER
 * @version 1.0
 */
@ChannelPipelineCoverage("one")
public class ChannelExceptionHandler extends SimpleChannelHandler {
    /**
     * <p>
     * It will be used to log all activity and errors.
     * </p>
     */
    private static final Log LOGGER = LogManager.getLog(ChannelExceptionHandler.class.getName());

    /**
     * Default empty constructor.
     */
    public ChannelExceptionHandler() {
    }

    /**
     * <p>
     * Method called when an exception has been thrown during channel operation. It should log the exception.
     * </p>
     *
     * @param ctx channel handler context, can't be null
     * @param e message event, can't be null
     * @throws IllegalArgumentException if context or event is null
     */
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        final String signature = "ChannelExceptionHandler#exceptionCaught";
        Helper.logEnter(signature, new String[] {"ChannelHandlerContext", "ExceptionEvent"}, new Object[] {ctx, e},
                LOGGER);
        Helper.checkNull(ctx, "context", signature, LOGGER);
        Helper.checkNull(e, "exception event", signature, LOGGER);
        Helper.logError(e.getCause(), signature, LOGGER);
        Helper.logExit(signature, null, LOGGER);
    }
}
